export default {
    formatDate: function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
        const pad = (num) => num.toString().padStart(2, '0');

        return format
            .replace(/YYYY/g, date.getFullYear())
            .replace(/MM/g, pad(date.getMonth() + 1))
            .replace(/DD/g, pad(date.getDate()))
            .replace(/HH/g, pad(date.getHours()))
            .replace(/mm/g, pad(date.getMinutes()))
            .replace(/ss/g, pad(date.getSeconds()));
    },

    getUrlParams: function () {
        const params = {};
        const queryString = window.location.search.substring(1); // 去掉问号
        const pairs = queryString.split('&');

        pairs.forEach(pair => {
            const [key, value] = pair.split('=');
            if (key) {
                params[decodeURIComponent(key)] = decodeURIComponent(value || '');
            }
        });

        return params;
    },

    tax2Map: function (list) {
        const keys = []
        const resMap = _.reduce(list, (map, item) => {
            keys.push(item.year)
            map[item.year] = _.reduce(item.monthData, (monthMap, mItem) => {
                monthMap[mItem.name] = mItem.value
                return monthMap
            }, {
                total: item.total
            })
            return map
        }, {})
        return {
            map: resMap,
            keys
        }
    },

    formatHorital: function (list, key, prop) {
        return _.reduce(list, (map, item) => {
            map[item[key]] = {
                ...item[prop]
            }
            return map
        }, {})
    },

    sortKeys: function (obj) {
        return Object.keys(obj).sort((a, b) => b - a)
    },

    /**
   * 用于整数的显示 次数 数量 张 月 天 条数等
   * @param {*} num 原始返回值
   * @param {*} showNo 为空时是否显示为未知 ‘-’  目前仅条数时为true
   * @returns 
   */
    formatInt: function formatInt(num, showNo = false) {
        if (num === undefined || num === null || num === '') {
            return showNo ? '-' : 0
        }
        const number = parseFloat(+num);
        if (isNaN(number)) {
            return num; // 非空且非数字原样返回
        }
        return number.toFixed(0)
    },

    // 百分比
    formatPercent: function formatPercent(num) {
        // 为空时显示为-
        if (num === undefined || num === null || num === '') {
            return '-'
        }
        const number = parseFloat(+num);
        if (number === 0) return 0 // 0时显示为0
        if (isNaN(number)) {
            return num; // 非空且非数字原样返回
        }
        return number.toFixed(2) + '%'
    },

    // 金额相关
    formatAmountToUnits: function formatAmountToUnits(num) {
        // 为空时显示为0
        if (num === undefined || num === null || num === '') {
            return 0
        }

        const number = parseFloat(+num);
        if (isNaN(number)) {
            return num; // 非空且非数字原样返回
        }
        if (number === 0) return 0;
        // 3. 根据数量级转换
        if (Math.abs(number) >= 1000000000000) { // 1亿以上
            return (number / 1000000000000).toFixed(2) + '万亿';
        } else if (Math.abs(number) >= 100000000) { // 1亿以上
            return (number / 100000000).toFixed(2) + '亿';
        } else if (Math.abs(number) >= 10000) { // 1万以上
            return (number / 10000).toFixed(2) + '万';
        } else {
            // 小于1万，直接返回原数字
            return number.toFixed(2);
        }
    },

    getDateDiff: function (dateStr1) {
        const date1 = new Date(dateStr1);
        const date2 = new Date();
        const timeDiff = Math.abs(date2.getTime() - date1.getTime());
        const dayDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
        if (dayDiff > 365 * 3) {
            return '-'
        } else {
            return dateStr1
        }
    },

    displayItmVal: function (props = {}, item = {}) {
        const { formatter: originFormatter, key = '' } = props
        const formatter = originFormatter || (val => val)
        const realVal = formatter(item[key])
        return realVal == null || realVal === '' ? '-' : realVal
    }
}
